package com.kikyou.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName weChatUrlReques
 * @Description T0D0
 * @Author ykq11
 * @Date 2022/8/22 20:25
 * @Version 1.0
 **/
@Component
public class WeChatUrlReques {
    @Value("${wechat.appID}")
    private String appIdStr;
    @Value("${wechat.appsecret}")
    private String appsecretStr;
    public final static Map<String, String> QING_HUA_MAP = new HashMap<>();
    private final static List<String> QING_HUA_LIST = new ArrayList<>();
    //这里报个错  注意token需要自己获取，每个接口量太小了
    String token="0pI2HiNSNg4YSsHM";
    public String getAccess_token(){
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appIdStr+"&secret="+appsecretStr;
        //获取access_token
        JSONObject jsonObject = urlRequestFormat(url);
        String access_token= (String) jsonObject.get("access_token");
        return access_token;
    }
    //内江天气
    public Map<String, Object> getWeather(String city){
        //暂时没处理get请求中文乱码，复制到浏览器改完字再复制回来
        String tianqiurl="https://www.yiketianqi.com/free/day?appid=42784621&appsecret=EBQd4N6d&unescape=1&city="+city;
        String tianqiAuditUrl="https://devapi.qweather.com/v7/indices/1d?type=3,5,8,11,13&location=101270101&key=1b18e8d7329e4cfc90d7b5f891075d03";
        Map<String, Object> weatherMap=new HashMap<>();
        JSONObject jsonObject = urlRequestFormat(tianqiurl);
        JSONObject tianqiObject = urlRequestFormat(tianqiAuditUrl);
        JSONArray jsonArray = JSONArray.parseArray(tianqiObject.get("daily").toString ());
        for ( Object item : jsonArray){
            JSONObject jsonItemArray = JSONArray.parseObject(item.toString ());
            if(jsonItemArray.get ( "type" ).equals ( "3" )){
                weatherMap.put("cy",jsonItemArray.get ( "text" ));
            }
            if(jsonItemArray.get ( "type" ).equals ( "5" )){
                weatherMap.put("zy",jsonItemArray.get ( "text" ));
            }
            if(jsonItemArray.get ( "type" ).equals ( "8" )){
                weatherMap.put("sh",jsonItemArray.get ( "text" ));
            }
            if(jsonItemArray.get ( "type" ).equals ( "11" )){
                weatherMap.put("kt",jsonItemArray.get ( "text" ));
            }
            if(jsonItemArray.get ( "type" ).equals ( "13" )){
                weatherMap.put("hz",jsonItemArray.get ( "text" ));
            }
        }
        weatherMap.put("city",jsonObject.get("city"));
        weatherMap.put("date",jsonObject.get("date"));
        weatherMap.put("week",jsonObject.get("week"));
        weatherMap.put("update_time",jsonObject.get("update_time"));
        //天气
        weatherMap.put("wea",jsonObject.get("wea"));
        //实时温度
        weatherMap.put("tem",jsonObject.get("tem"));
        //高温
        weatherMap.put("tem_day",jsonObject.get("tem_day"));
        //低温
        weatherMap.put("tem_night",jsonObject.get("tem_night"));
        System.out.println (weatherMap);
        return weatherMap;
    }
    //情话获取
     public  Map<String, Object> getQingHua(){
         String qinghuaqiurl ="https://v2.alapi.cn/api/qinghua?token="+token;
         Map<String, Object> qinghuaMap=new HashMap<>();
         String result= HttpUtil.createGet(qinghuaqiurl).execute().body();
         System.out.println (result);
         JSONObject jsonObject = JSONObject.parseObject(result);
         try {
             JSONObject jsonObjectData=JSONObject.parseObject(jsonObject.get("data").toString());
             String str= jsonObjectData.get("content").toString();
             if(str.contains("傻逼")){
                 str="今天的你也特别漂亮哦";
             }
             qinghuaMap.put("content",str);
         }catch (Exception e) {
             e.printStackTrace();
         }
         return qinghuaMap;
     }

    public static void main (String[] args) {
        new WeChatUrlReques().getQingHua ();
    }
    //接口量小，凌晨统一请求三条放到list中
    @Scheduled(cron = "0 40 00 * * ? ")
    public void getQingHuaList() throws Exception {
        for (int i = 0; i <4 ; i++) {
            QING_HUA_LIST.add((String) getQingHua().get("content"));
            Thread.sleep( 15000);
        }
        QING_HUA_MAP.put("早",QING_HUA_LIST.get(0));
        QING_HUA_MAP.put("中",QING_HUA_LIST.get(1));
        QING_HUA_MAP.put("下",QING_HUA_LIST.get(2));
        QING_HUA_MAP.put("晚",QING_HUA_LIST.get(3));
    }
    //提前清空List
    @Scheduled(cron = "0 20 00 * * ? ")
    public void getQingHuaListInit() throws Exception {
        if(QING_HUA_LIST.size()>0){
            QING_HUA_LIST.clear();
        }
    }

    //网易云歌曲评论获取
     public  Map<String, Object> getCloundSong() {
        String cloundSonUrl=" https://v2.alapi.cn/api/comment?token="+token;
        String result= HttpUtil.createGet(cloundSonUrl).execute().body();
        Map<String, Object> cloundSongMap=new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(result);
         //获取数据
        JSONObject jsonObjectData = JSONObject.parseObject(jsonObject.get("data").toString());
        //获取歌曲名字
        cloundSongMap.put("title",jsonObjectData.get("title"));
        //获取评论
        cloundSongMap.put("content",jsonObjectData.get("comment_content"));
        return cloundSongMap;
     }
 //微博热搜获取
    public  Map<String, Object> getWeiBoHost(){
        String accUrl = "https://www.coderutil.com/api/resou/v1/weibo";
        // 添加请求头信息
        Map<String, String> heads = new HashMap<>();
        //授权码需要自己注册获取
        heads.put("access-key","5eec086d935e47cb37c9509e7c6bd5e1");
        heads.put("secret-key","b629a5a3fc84462dd88f33714edba333");
        HttpResponse response = HttpRequest.get(accUrl)
                .headerMap(heads, false)
                .timeout(5 * 60 * 1000)
                .execute();
      String weiboContent = response.body();
         Map<String, Object> weiboContentMap=new HashMap<>();
         JSONObject jsonObject = JSONObject.parseObject(weiboContent);
         //获取数据
         StringBuffer weiboString=new StringBuffer();
         JSONArray array = jsonObject.getJSONArray("data");
         for (int i = 0; i < array.size(); i++) {
             JSONObject jo = array.getJSONObject(i);
             weiboString.append((i+1)+".");
             weiboString.append(jo.getString("keyword")+"\n");
         }
         weiboContentMap.put("weiboContent",weiboString.toString());
         return weiboContentMap;
    }
    public static JSONObject urlRequestFormat(String url) {
        String result= HttpUtil.createGet(url).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }
}
